<template>
  <common-layout>
    <div class="top">
      <div class="header">
        <img
          alt="logo"
          class="logo"
          :src="imageUrl('/images/adminImg/logo.png')"
        />
        <span class="title">{{ systemName }}</span>
      </div>
      <!-- <div class="desc">Ant Design 是西湖区最具影响力的 Web 设计规范</div> -->
    </div>
    <div class="login">
      <a-form @submit="onSubmit" :form="form">
        <!-- <a-alert type="error" :closable="true" v-if="error" :message="error" @close='onClose' showIcon style="margin-bottom: 24px;" /> -->
        <a-form-item style="margin-top: 60px">
          <a-input
            autocomplete="autocomplete"
            size="large"
            placeholder="请输入用户名"
            v-decorator="[
              'userName',
              {
                rules: [{ required: true, message: '请输入账户名' }],
              },
            ]"
          >
            <!-- initialValue 默认值-->
            <a-icon slot="prefix" type="user" />
          </a-input>
        </a-form-item>
        <a-form-item>
          <a-input
            size="large"
            placeholder="请输入密码"
            autocomplete="autocomplete"
            type="password"
            v-decorator="[
              'passWord',
              {
                rules: [{ required: true, message: '请输入密码' }],
              },
            ]"
          >
            <a-icon slot="prefix" type="lock" />
          </a-input>
        </a-form-item>

        <div>
          <a-checkbox :checked="true">自动登录</a-checkbox>
          <!-- <a style="float: right">忘记密码</a> -->
        </div>
        <a-form-item>
          <a-button
            :loading="logging"
            style="width: 100%; margin-top: 24px"
            size="large"
            htmlType="submit"
            type="primary"
            >登录</a-button
          >
        </a-form-item>
        <!-- <div>
          其他登录方式
          <a-icon class="icon" type="alipay-circle" />
          <a-icon class="icon" type="taobao-circle" />
          <a-icon class="icon" type="weibo-circle" />
          <router-link style="float: right" to="/dashboard/workplace" >注册账户</router-link>
        </div> -->
      </a-form>
    </div>
  </common-layout>
</template>

<script>
javascript: window.history.forward(1);
import CommonLayout from "@/layouts/CommonLayout";
import { login, getRoutesConfig, checkRole } from "@/services/user";
import { mapMutations } from "vuex";

export default {
  name: "Login",
  components: { CommonLayout },
  data() {
    return {
      logging: false,
      error: "",
      form: this.$form.createForm(this),
    };
  },
  created() {},

  computed: {
    systemName() {
      return this.$store.state.setting.systemName;
    },
  },
  methods: {
    ...mapMutations("account", [
      "setUser",
      "setPermissions",
      "setRoles",
      "setFirst",
      "setHeader",
    ]),

    onSubmit(e) {
      e.preventDefault();
      this.form.validateFields((err) => {
        if (!err) {
          this.logging = true;
          const userName = this.form.getFieldValue("userName");
          const passWord = this.form.getFieldValue("passWord");
          login(userName, passWord).then(this.afterLogin);
        }
      });
    },
    async afterLogin(res) {
      const loginRes = res;
      if (loginRes.code == 200) {
        // const { user, permissions, roles, token } = loginRes.data;
        // this.setUser(user);
        // this.setPermissions([{ id: "admin", operation: ["add", "edit"] }]);
        // this.setRoles(roles);
        // setAuthorization( token );
        // 获取路由配置11

        let header = loginRes.data.header;
        this.setHeader(header);
        let res = await checkRole(); //获取权限信息
        if (res.code == 200) {
          const { user, permissions } = res.data;
          this.setUser(user);
          this.setPermissions(permissions);
          this.setFirst(true); //是否为第一次登录
          await getRoutesConfig(); //获取动态路由
          this.logging = false;
          let redirect = this.$route.query.redirect;
          if (redirect) {
            this.$router.push(redirect); //前往重定向页面
          } else {
            this.$router.push("/Dashboard/workbench"); //前往工作台
          }

          this.$message.success(loginRes.message, 3);
        }
      } else {
        this.error = loginRes.message;
      }
      this.logging = false;
    },
    onClose() {
      this.error = false;
    },
  },
};
</script>

<style lang="less" scoped>
.common-layout {
  .top {
    text-align: center;
    .header {
      height: 44px;
      line-height: 44px;
      a {
        text-decoration: none;
      }
      .logo {
        height: 44px;
        vertical-align: top;
        margin-right: 16px;
      }
      .title {
        font-size: 33px;
        color: @title-color;
        font-family: "Myriad Pro", "Helvetica Neue", Arial, Helvetica,
          sans-serif;
        font-weight: 600;
        position: relative;
        top: 2px;
      }
    }
    .desc {
      font-size: 14px;
      color: @text-color-second;
      margin-top: 12px;
      margin-bottom: 40px;
    }
  }
  .login {
    width: 368px;
    margin: 0 auto;
    @media screen and (max-width: 576px) {
      width: 95%;
    }
    @media screen and (max-width: 320px) {
      .captcha-button {
        font-size: 14px;
      }
    }
    .icon {
      font-size: 24px;
      color: @text-color-second;
      margin-left: 16px;
      vertical-align: middle;
      cursor: pointer;
      transition: color 0.3s;

      &:hover {
        color: @primary-color;
      }
    }
  }
}
</style>
